package com.teaching.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.teaching.Common.R;
import com.teaching.Entity.Activity;
import com.teaching.Service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    ActivityService activityService;

    /**
     * 获取轮播图
     * <p>
     * activityClass:获取活动类型
     *
     * @param data
     * @return
     */
    @PostMapping("/slideshow")
    public R<List<Activity>> slideshow(@RequestBody Map<String, Object> data) {
        log.info("接收到请求：" + data.isEmpty());
        String activityClass = (String) data.get("activityClass");
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(activityClass), Activity::getActivityClass, activityClass);
        List<Activity> list = activityService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 新增活动
     *
     * @param activity
     * @return
     */
    @PostMapping("/addactivity")
    public R<String> addActivity(@RequestBody Activity activity) {
        activityService.save(activity);
        return R.success("新增成功");
    }
}
